package com.moody.web;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.moody.hibernate.domain.ReportModel;
import com.moody.hibernate.dao.CustInfoDAO;
import com.moody.hibernate.domain.CustInfo;
import com.moody.hibernate.domain.EnterBalanceSheet;
import com.moody.hibernate.domain.EnterCashFlow;
import com.moody.hibernate.domain.EnterIncome;
import com.moody.hibernate.domain.FinanBalanceSheet;
import com.moody.hibernate.domain.FinanFinancialIndexSheet;
import com.moody.hibernate.domain.FinanIncomeSheet;
import com.moody.hibernate.domain.PublicBalanceSheet;
import com.moody.hibernate.domain.PublicFinancialIndex;
import com.moody.hibernate.domain.ReportData;
import com.moody.hibernate.domain.ReportDataId;
import com.moody.hibernate.domain.ReportList;
import com.moody.hibernate.domain.ReportListId;
import com.moody.hibernate.domain.ResultDisplayForFindCustomerInFinancialAnalysis;
import com.moody.hibernate.domain.SmallBalanceSheet;
import com.moody.hibernate.domain.SmallCashFlowSheet;
import com.moody.hibernate.domain.SmallIncomeSheet;
import com.moody.hibernate.domain.UserInfo;
import com.moody.init.DataDictionary;
import com.moody.init.ReportModelInit;
import com.moody.service.CustomerService;
import com.moody.service.ObligationService;
import com.moody.service.SystemService;
import com.moody.util.CalendarUtil;
import com.moody.util.CopyUtil;

/*
 * Copyright © Moody analytics
 *
 * @Author: Yan Jian
 * @Time: 2011-10-10 下午02:50:11
 * @Modified by: 
 * @Description: 
 */

@Controller
@RequestMapping("/finance")
public class FinancialAnalysisController {
	private static final Logger log = LoggerFactory
			.getLogger(FinancialAnalysisController.class);

	private static final List<CustInfo> lastOneGeneralBalance = null;

	WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
	ServletContext context = web.getServletContext();  
	WebApplicationContext ctx  = WebApplicationContextUtils.getWebApplicationContext(context);

	private CustInfoDAO custInfoDao = CustInfoDAO
			.getFromApplicationContext(ctx);

	@Resource
	private CustomerService customerService;	
	@Resource
	private SystemService systemService;
	@Resource
	private ObligationService obligationService;
	
	@RequestMapping("/exportExcel")
		public void exportExcel(HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException{
		response.setContentType("application/vnd.ms-excel;charset=utf-8");  
		 
		OutputStream os = response.getOutputStream();
		WritableWorkbook wwb = Workbook.createWorkbook(os);
		
		String colId = request.getParameter("colId");
		StringTokenizer tokenizer = new StringTokenizer(colId,"-");
		List<String> colIdList = new LinkedList<String>();
		while(tokenizer.hasMoreElements()){
			colIdList.add(tokenizer.nextToken());
		}
		
		List<CustInfo> resultList = new LinkedList<CustInfo>();
		List<ResultDisplayForFindCustomerInFinancialAnalysis> trueResultList = null;
		
//		String custId = (String) request.getSession().getAttribute("custId");
		
		String custType = (String) request.getSession().getAttribute("custType"); 
		String custScope = (String) request.getSession().getAttribute("custScope");
		String orgCode = (String) request.getSession().getAttribute("orgCode");
		String industryType = (String) request.getSession().getAttribute("industryType");
		String creditLevel = (String) request.getSession().getAttribute("creditLevel");
		String defaultStat = (String) request.getSession().getAttribute("defaultStat");
		String exposureType = (String) request.getSession().getAttribute("exposureType");
		String accountMonth = (String) request.getSession().getAttribute("accountMonth");

//		CustInfo custBase = customerService.findCustByCustId(custId);

		
		//由于数据库中直接存储了“风险暴露类型”的文字内容，所以查询前，需转换exposureType
		String exposureTypeString = "";
		if(!exposureType.equals("")){
			exposureTypeString = DataDictionary.getInstance().getNameByCodeAndId("ExposureType", exposureType);
		}
		
//		String queryType = custType;
//		if(queryType.equals("04"))//小企业法人类型也为企业法人（01）
//			queryType = "01";

		Map<String,String> condition = new LinkedHashMap<String,String>();
		if(!custType.equals(""))condition.put("custType", custType);
		if(!custScope.equals(""))condition.put("custScope", custScope);
		if(!orgCode.equals(""))condition.put("inputOrgId", orgCode);
		if(!industryType.equals(""))condition.put("industryType", industryType);
		if(!creditLevel.equals(""))condition.put("creditLevel", creditLevel);
		if(!defaultStat.equals(""))condition.put("defaultStat", defaultStat);
		if(!exposureTypeString.equals(""))condition.put("exposureType", exposureTypeString);
		
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
		resultList = customerService.findCustByCondition(condition,userId,orgId);

//		resultList = customerService.findBiggestCustByCondition(custType,custScope, exposureTypeString, orgCode,industryType, creditLevel,defaultStat,userId,orgId);

		Iterator<CustInfo> it = resultList.iterator();
		while(it.hasNext()){
			CustInfo cust = it.next();
			
//			// 如果是企业法人，则需要移除小企业法人
//			if (custType.equals("01")) {
//				if (cust.getCustType() != null
//						&& (("01").equals(cust.getCustScope()) || ("02").equals(cust.getCustScope()))) {
//					it.remove();
//					continue;
//				}
//			}
//			// 如果是小企业法人，则需要移除企业法人
//			else if(custType.equals("04")){
//				if (cust.getCustType() != null
//						&& (("03").equals(cust.getCustScope()) || ("04").equals(cust
//								.getCustScope()) || ("05").equals(cust.getCustScope()))) {
//					it.remove();
//					continue;
//				}
//			}
//			//为了将被对比客户放在第一行，所以需要移除后插入到0位置
//			if(cust.getCustId().equals(custId)) {
//				custBase = cust;
//				it.remove();
//				continue;
//			}
			
			if(cust.getCustType() != null && !"".equals(cust.getCustType()))cust.setCustType(DataDictionary.getInstance().getNameByCodeAndId("CustType", cust.getCustType()));
			if(cust.getCustScope() != null && !"".equals(cust.getCustScope()))cust.setCustScope(DataDictionary.getInstance().getNameByCodeAndId("CustScope", cust.getCustScope()));
			if(cust.getIndustryType() !=null && !"".equals(cust.getIndustryType()))cust.setIndustryType(DataDictionary.getInstance().getNameByCodeAndId("IndustryType", cust.getIndustryType()));
			if(cust.getDefaultStat() !=null && !"".equals(cust.getDefaultStat()))cust.setDefaultStat(DataDictionary.getInstance().getNameByCodeAndId("DefaultStat", cust.getDefaultStat()));
			cust.setBalance(obligationService.calDebtBalanceSumByCustId(cust.getCustId()));
			if (cust.getInputOrgId() != null && !cust.getInputOrgId().equals("")&& systemService.findOrgById(cust.getInputOrgId()) != null)
				cust.setInputOrgId(systemService.findOrgById(cust.getInputOrgId()).getOrgname());

			
		}
//		if(custBase.getCustType() != null && !"".equals(custBase.getCustType()))custBase.setCustType(DataDictionary.getInstance().getNameByCodeAndId("CustType", custBase.getCustType()));
//		if(custBase.getCustScope() != null && !"".equals(custBase.getCustScope()))custBase.setCustScope(DataDictionary.getInstance().getNameByCodeAndId("CustScope", custBase.getCustScope()));
//		if(custBase.getIndustryType() !=null && !"".equals(custBase.getIndustryType()))custBase.setIndustryType(DataDictionary.getInstance().getNameByCodeAndId("IndustryType", custBase.getIndustryType()));
//		if(custBase.getDefaultStat() !=null && !"".equals(custBase.getDefaultStat()))custBase.setDefaultStat(DataDictionary.getInstance().getNameByCodeAndId("DefaultStat", custBase.getDefaultStat()));
//		custBase.setBalance(obligationService.calDebtBalanceSumByCustId(custBase.getCustId()));
//		if (custBase.getInputOrgId() != null && !custBase.getInputOrgId().equals("")&& systemService.findOrgById(custBase.getInputOrgId()) != null)
//			custBase.setInputOrgId(systemService.findOrgById(custBase.getInputOrgId()).getOrgname());
//		
//		resultList.add(0, custBase);
		
		trueResultList = new LinkedList<ResultDisplayForFindCustomerInFinancialAnalysis>();
		for(Object custInfo : resultList){
//			((CustInfo) custInfo).setCustType(DataDictionary.getInstance().getNameByCodeAndId("QueryCustType", custType));
			ResultDisplayForFindCustomerInFinancialAnalysis displayItem = new ResultDisplayForFindCustomerInFinancialAnalysis();
			if(CopyUtil.copy(custInfo, displayItem)){
				if(!custType.equals("") && !accountMonth.equals("")){
					List<ReportData> financialIndexList = customerService.findFinancialIndexInCondition(((CustInfo) custInfo).getCustId(), accountMonth);
					if(financialIndexList.size() > 0){
						CopyUtil.copyFinancialIndex(financialIndexList,displayItem);
						trueResultList.add(displayItem);
					}
				}
			}
		}
		trueResultList.add(CalendarUtil.getAverage(trueResultList));
		
		//excel文件生成：
		String reportNo = null;
		if(custType.equals("01")) reportNo = "0105";
		else if(custType.equals("02")) reportNo = "0205";
		else if(custType.equals("03")) reportNo = "0303";
		else if(custType.equals("04")) reportNo = "0505";
		Map<String,String> map = customerService.findReportDisplayName(reportNo);
		map.put("custType","客户类型");
		map.put("custScope","客户规模");
		map.put("industryType","所属行业");
		map.put("inputOrgId","所属机构");
		map.put("creditLevel","信用等级");
		map.put("exposureType","风险暴露分类");
		map.put("defaultStat","违约状态");
		
		
		String fileName = "Export";
		//fileName = new String(fileName.getBytes(),"utf-8");
		log.info("filename: "+fileName);
		response.addHeader("Content-Disposition", "attachment;filename="+fileName+".xls");

		int displayLines = colIdList.size() + 3;
		
		
		try {

			//新建一张表
			WritableSheet wsheet = wwb.createSheet("查询结果列表",0);
			for(int i = 0; i < displayLines; i++){
				wsheet.setColumnView(i, 20); // 设置列的宽度
			}
			
			//设置表头
			WritableFont wf = new WritableFont(WritableFont.ARIAL, 11,
					WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.WHITE); // 定义格式 字体 下划线 斜体 粗体 颜色
			WritableCellFormat wcf = new WritableCellFormat(wf); // 单元格定义
			wcf.setBackground(jxl.format.Colour.SKY_BLUE); // 设置单元格的背景颜色
			wcf.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
			
			WritableFont wf1 = new WritableFont(WritableFont.ARIAL, 10,
					WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
					jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
			WritableCellFormat wcf1 = new WritableCellFormat(wf1); // 单元格定义
			wcf1.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式
					

			Label label = new Label(0,0,"编号",wcf);
			wsheet.addCell(label);
			label = new Label(1,0,"客户编号",wcf);
			wsheet.addCell(label);
			label = new Label(2,0,"客户名称",wcf);
			wsheet.addCell(label);
			for(int i = 3; i<displayLines;i++){
				label = new Label(i,0,map.get(colIdList.get(i-3)),wcf);
				wsheet.addCell(label);
			}
			
			
			Iterator<ResultDisplayForFindCustomerInFinancialAnalysis> it1 = trueResultList.iterator();
			int lineNum = 1;
			Class resultClass = ResultDisplayForFindCustomerInFinancialAnalysis.class;
			while(it1.hasNext()){
				ResultDisplayForFindCustomerInFinancialAnalysis item = it1.next();
				
				label = new Label(0,lineNum,""+lineNum,wcf1);
				wsheet.addCell(label);
				label = new Label(1,lineNum,item.getCustNo(),wcf1);
				wsheet.addCell(label);
				label = new Label(2,lineNum,item.getCustName(),wcf1);
				wsheet.addCell(label);
				for(int i = 3; i < displayLines; i++){
					try {
						Object indexObject = resultClass.getDeclaredField(colIdList.get(i-3)).get(item);
						if(indexObject != null && !indexObject.toString().equals("null")) label = new Label(i,lineNum,indexObject.toString(),wcf1);
						else label = new Label(i,lineNum,"",wcf1);
						wsheet.addCell(label);
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (NoSuchFieldException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				lineNum++;
				
			}
			
			wwb.write();
			wwb.close();
			
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
		
		  
		  os.close();
		  response.flushBuffer();
		
	}

	// 查找客户
	@RequestMapping("/findCustomer")
	public void findCustomer(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String custType = request.getParameter("custType");
		String custName = request.getParameter("custName");
		String custNo = request.getParameter("custNo");
		String inputOrgId = request.getParameter("inputOrgId");

		List<CustInfo> list = new ArrayList<CustInfo>();
		List resultList = new ArrayList<CustInfo>();
		
//		String queryType = custType;
//		if(queryType.equals("04"))
//			queryType = "01";

		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
		Map<String,String> condition = new LinkedHashMap<String,String>();
		condition.put("custType", custType);
		condition.put("custName", custName);
		condition.put("custNo", custNo);
		condition.put("inputOrgId", inputOrgId);
		list = customerService.findCustByCondition(condition, userId, orgId);

		for (CustInfo cust : list) {
			cust.setCustType(DataDictionary.getInstance().getNameByCodeAndId("CustType", cust.getCustType()));
//			
//			// 如果是企业法人，则需要移除小企业法人
//			if (custType.equals("01")) {
//				if (cust.getCustType() != null
//						&& (("01").equals(cust.getCustScope()) || ("02").equals(cust.getCustScope()))) {
//				}else{
//					resultList.add(cust);
//				}
//			}
//			// 如果是小企业法人，则需要移除企业法人
//			else if(custType.equals("04")){
//				if (cust.getCustType() != null
//						&& (("03").equals(cust.getCustScope()) || ("04").equals(cust
//								.getCustScope()) || ("05").equals(cust.getCustScope()))) {
//				}else{
//					resultList.add(cust);
//				}
//			}else{
//				resultList.add(cust);
//			}
		}
		
		
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();

		Map<String, Object> resultMap = new HashMap<String, Object>(3);
		resultMap.put("success", "true");
		resultMap.put("result", list);
		JSONObject resultJO = JSONObject.fromObject(resultMap);
		out.print(resultJO);
	}

	// 查找客户
	@RequestMapping("/findPeerComparation")
	public void findPeerComparation(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		List<CustInfo> resultList = new LinkedList<CustInfo>();
		List<ResultDisplayForFindCustomerInFinancialAnalysis> trueResultList = null;
		
//		String custId = (String) request.getSession().getAttribute("custId");
//		CustInfo custBase = customerService.findCustByCustId(custId);
		String custType = request.getParameter("custType");
		String custScope = request.getParameter("custScope");
		String orgCode = request.getParameter("orgCode");
		String industryTypeOne = request.getParameter("industryTypeLevelOne");
		String industryTypeTwo = request.getParameter("industryTypeLevelTwo");
		String creditLevel = request.getParameter("pdValue");
		String defaultStat = request.getParameter("defaultStat");
		String exposureType = request.getParameter("exposureType");
		String accountMonth = request.getParameter("accountMonth");
		
		String industryType = "";
		if(industryTypeTwo.equals("")){
			if(!industryTypeOne.equals("")) industryType = new StringTokenizer(industryTypeOne).nextToken();
		}
		else industryType = industryTypeTwo;
		
		request.getSession().setAttribute("custType", custType);
		request.getSession().setAttribute("custScope", custScope);
		request.getSession().setAttribute("orgCode", orgCode);
		request.getSession().setAttribute("industryType", industryType);
		request.getSession().setAttribute("creditLevel", creditLevel);
		request.getSession().setAttribute("defaultStat", defaultStat);
		request.getSession().setAttribute("exposureType", exposureType);
		request.getSession().setAttribute("accountMonth", accountMonth);
		
		//由于数据库中直接存储了“风险暴露类型”的文字内容，所以查询前，需转换exposureType
		String exposureTypeString = "";
		if(!exposureType.equals("")){
			exposureTypeString = DataDictionary.getInstance().getNameByCodeAndId("ExposureType", exposureType);
		}
		
//		String queryType = custType;
//		if(queryType.equals("04"))//小企业法人类型也为企业法人（01）
//			queryType = "01";

		Map<String,String> condition = new LinkedHashMap<String,String>();
		if(!custType.equals(""))condition.put("custType", custType);
		if(!custScope.equals(""))condition.put("custScope", custScope);
		if(!orgCode.equals(""))condition.put("inputOrgId", orgCode);
		if(!industryType.equals(""))condition.put("industryType", industryType);
		if(!creditLevel.equals(""))condition.put("creditLevel", creditLevel);
		if(!defaultStat.equals(""))condition.put("defaultStat", defaultStat);
		if(!exposureTypeString.equals(""))condition.put("exposureType", exposureTypeString);
		
		UserInfo user = (UserInfo) request.getSession().getAttribute("logonUser");
		String userId = user.getUserId();
		String orgId = user.getBelongorg();
		resultList = customerService.findCustByCondition(condition,userId,orgId);

//		resultList = customerService.findBiggestCustByCondition(custType,custScope, exposureTypeString, orgCode,industryType, creditLevel,defaultStat,userId,orgId);

		Iterator<CustInfo> it = resultList.iterator();
		while(it.hasNext()){
			CustInfo cust = it.next();
			
//			// 如果是企业法人，则需要移除小企业法人
//			if (custType.equals("01")) {
//				if (cust.getCustType() != null
//						&& (("01").equals(cust.getCustScope()) || ("02").equals(cust.getCustScope()))) {
//					it.remove();
//					continue;
//				}
//			}
//			// 如果是小企业法人，则需要移除企业法人
//			else if(custType.equals("04")){
//				if (cust.getCustType() != null
//						&& (("03").equals(cust.getCustScope()) || ("04").equals(cust
//								.getCustScope()) || ("05").equals(cust.getCustScope()))) {
//					it.remove();
//					continue;
//				}
//			}
//			//为了将被对比客户放在第一行，所以需要移除后插入到0位置
//			if(cust.getCustId().equals(custId)) {
//				custBase = cust;
//				it.remove();
//				continue;
//			}
			
			if(cust.getCustType() != null && !"".equals(cust.getCustType()))cust.setCustType(DataDictionary.getInstance().getNameByCodeAndId("CustType", cust.getCustType()));
			if(cust.getCustScope() != null && !"".equals(cust.getCustScope()))cust.setCustScope(DataDictionary.getInstance().getNameByCodeAndId("CustScope", cust.getCustScope()));
			if(cust.getIndustryType() !=null && !"".equals(cust.getIndustryType()))cust.setIndustryType(DataDictionary.getInstance().getNameByCodeAndId("IndustryType", cust.getIndustryType()));
			if(cust.getDefaultStat() !=null && !"".equals(cust.getDefaultStat()))cust.setDefaultStat(DataDictionary.getInstance().getNameByCodeAndId("DefaultStat", cust.getDefaultStat()));
			cust.setBalance(obligationService.calDebtBalanceSumByCustId(cust.getCustId()));
			if (cust.getInputOrgId() != null && !cust.getInputOrgId().equals("")&& systemService.findOrgById(cust.getInputOrgId()) != null)
				cust.setInputOrgId(systemService.findOrgById(cust.getInputOrgId()).getOrgname());

			
		}
//		if(custBase.getCustType() != null && !"".equals(custBase.getCustType()))custBase.setCustType(DataDictionary.getInstance().getNameByCodeAndId("CustType", custBase.getCustType()));
//		if(custBase.getCustScope() != null && !"".equals(custBase.getCustScope()))custBase.setCustScope(DataDictionary.getInstance().getNameByCodeAndId("CustScope", custBase.getCustScope()));
//		if(custBase.getIndustryType() !=null && !"".equals(custBase.getIndustryType()))custBase.setIndustryType(DataDictionary.getInstance().getNameByCodeAndId("IndustryType", custBase.getIndustryType()));
//		if(custBase.getDefaultStat() !=null && !"".equals(custBase.getDefaultStat()))custBase.setDefaultStat(DataDictionary.getInstance().getNameByCodeAndId("DefaultStat", custBase.getDefaultStat()));
//		custBase.setBalance(obligationService.calDebtBalanceSumByCustId(custBase.getCustId()));
//		if (custBase.getInputOrgId() != null && !custBase.getInputOrgId().equals("")&& systemService.findOrgById(custBase.getInputOrgId()) != null)
//			custBase.setInputOrgId(systemService.findOrgById(custBase.getInputOrgId()).getOrgname());
//		
//		resultList.add(0, custBase);
		
		trueResultList = new LinkedList<ResultDisplayForFindCustomerInFinancialAnalysis>();
		for(Object custInfo : resultList){
//			((CustInfo) custInfo).setCustType(DataDictionary.getInstance().getNameByCodeAndId("QueryCustType", custType));
			ResultDisplayForFindCustomerInFinancialAnalysis displayItem = new ResultDisplayForFindCustomerInFinancialAnalysis();
			if(CopyUtil.copy(custInfo, displayItem)){
				if(!custType.equals("") && !accountMonth.equals("")){
					List<ReportData> financialIndexList = customerService.findFinancialIndexInCondition(((CustInfo) custInfo).getCustId(), accountMonth);
					if(financialIndexList.size() > 0){
						CopyUtil.copyFinancialIndex(financialIndexList,displayItem);
						trueResultList.add(displayItem);
					}
				}
			}
		}
		trueResultList.add(CalendarUtil.getAverage(trueResultList));
		
		
		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();

		Map<String, Object> resultMap = new HashMap<String, Object>(3);
		resultMap.put("success", "true");
		resultMap.put("result", trueResultList);
		JSONObject resultJO = JSONObject.fromObject(resultMap);
		out.print(resultJO);
	}

	// 列出客户所有财报
	@RequestMapping("/listReport")
	public void listReport(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String custId = request.getParameter("custId");

		request.getSession().removeAttribute("custId");
		request.getSession().setAttribute("custId", custId);

		List<ReportList> list = new ArrayList<ReportList>();

		list = customerService.findReportListByCustId(custId);
		List<Map<String, String>> reList = new ArrayList<Map<String, String>>();
		for (ReportList rl : list) {
			Map<String, String> map = new LinkedHashMap<String, String>();
			map.put("accountMonth", rl.getId().getAccountMonth());
			map.put("reportScope",
					DataDictionary.getInstance().getNameByCodeAndId(
							"ReportScope", rl.getId().getReportScope()));
			map.put("reportKind", DataDictionary.getInstance()
					.getNameByCodeAndId("ReportKind", rl.getReportKind()));
			map.put("reportCurrency", DataDictionary.getInstance()
					.getNameByCodeAndId("Currency", rl.getReportCurrency()));
			map.put("auditStatus", DataDictionary.getInstance()
					.getNameByCodeAndId("AuditStatus", rl.getAuditStatus()));
			reList.add(map);

		}

		response.setContentType("application/json; charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();

		JSONArray jo = JSONArray.fromObject(reList);

		out.print(jo);

	}

	// 查看财务简表和财务指标表
	@RequestMapping("/checkRecentGeneralReport")
	public String checkRecentGeneralReport(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		ReportList report = new ReportList();
		ReportListId reportId = new ReportListId();

		CustInfo currentCust = new CustInfo();

		String accountMonth = request.getParameter("accountMonth");
		String reportScopeName = request.getParameter("reportScope");
		if (accountMonth != null && reportScopeName != null) {
			request.removeAttribute("accountMonth");
			request.removeAttribute("reportScopeName");
			request.getSession().setAttribute("accountMonth", accountMonth);
			request.getSession().setAttribute("reportScopeName",
					reportScopeName);
		} else {
			accountMonth = request.getSession().getAttribute("accountMonth")
					.toString();
			reportScopeName = request.getSession()
					.getAttribute("reportScopeName").toString();
		}

		String reportScopeCode = DataDictionary.getInstance()
				.getDataDictionary().get("ReverseReportScope")
				.get(reportScopeName).getId().getItemNo();
		String custId = request.getSession().getAttribute("custId").toString();
		if (custId != null && !custId.equals(""))
			reportId.setCustId(custId);
		reportId.setAccountMonth(accountMonth);
		reportId.setReportScope(reportScopeCode);

		if (accountMonth != null && accountMonth != null
				&& reportScopeCode != null) {
			report = customerService.findReportListById(reportId);
		} else {
			report = (ReportList) request.getSession().getAttribute("report");
		}
		request.getSession().removeAttribute("report");

		request.getSession().setAttribute("report", report);

		List<ReportData> recentList = customerService
				.findRecentReportData(report);
		// model.addAttribute("recentGeneral", recentList);
		Map<String, List<ReportData>> recentGeneralMap = new LinkedHashMap<String, List<ReportData>>();
		List<ReportData> recentGeneralBalance = new LinkedList<ReportData>();
		List<ReportData> recentGeneralIncome = new LinkedList<ReportData>();
		List<ReportData> recentGeneralCashFLow = new LinkedList<ReportData>();

		currentCust = customerService.findCustByCustId(custId);

		currentCust.setCustType(DataDictionary.getInstance()
				.getNameByCodeAndId("CustType", currentCust.getCustType()));

		request.getSession().removeAttribute("CurrentCust");
		request.getSession().setAttribute("CurrentCust", currentCust);
		
		// 企业法人
		if (recentList != null && recentList.size() > 0
				&& recentList.get(0).getId().getReportNo().startsWith("01")) {
			for (ReportData rd : recentList) {
				if (rd.getId().getReportNo().equals(EnterBalanceSheet.REPORTNO)
						&& rd.getDisplayFlag().substring(0, 1).equals("1"))
					recentGeneralBalance.add(rd);
				else if (rd.getId().getReportNo().equals(EnterIncome.REPORTNO)
						&& rd.getDisplayFlag().substring(0, 1).equals("1"))
					recentGeneralIncome.add(rd);
				else if (rd.getId().getReportNo()
						.equals(EnterCashFlow.REPORTNO)
						&& rd.getDisplayFlag().substring(0, 1).equals("1"))
					recentGeneralCashFLow.add(rd);
				else {
					// log.info(arg0)
				}
			}
			
			//如果为空，从Model中去数据填充, 默认填充数据0.00
			if(recentGeneralBalance.size()<=0){
				List<ReportModel> balanceList = ReportModelInit.getInstance().getEnterBalanceSheetListFromModel();
				recentGeneralBalance = getEmptyReportData(report, balanceList,EnterBalanceSheet.REPORTNO);	
			}
			if(recentGeneralIncome.size() <= 0){
				List<ReportModel> incomeList = ReportModelInit.getInstance().getEnterIncomeFromModel();
				recentGeneralIncome = getEmptyReportData(report, incomeList,EnterIncome.REPORTNO);
			}
			if(recentGeneralCashFLow.size() <= 0 ){
				List<ReportModel> cashFlowList = ReportModelInit.getInstance().getEnterCashFlowFromModel();
				recentGeneralCashFLow = getEmptyReportData(report, cashFlowList,EnterCashFlow.REPORTNO);
			}
			

			recentGeneralBalance = resortReportDataOrder(recentGeneralBalance);
			recentGeneralMap.put("资产负债", recentGeneralBalance);
			recentGeneralMap.put("损益", recentGeneralIncome);
			recentGeneralMap.put("现金流量", recentGeneralCashFLow);

		}// 事业单位
		else if (recentList != null && recentList.size() > 0
				&& recentList.get(0).getId().getReportNo().startsWith("02")) {
			for (ReportData rd : recentList) {
				if (rd.getId().getReportNo()
						.equals(PublicBalanceSheet.REPORTNO)
						&& rd.getDisplayFlag().substring(0, 1).equals("1"))
					recentGeneralBalance.add(rd);
				else {
					// log.info(arg0)
				}

			}
			
			//如果为空，从Model中去数据填充, 默认填充数据0.00
			if(recentGeneralBalance.size()<=0){
				List<ReportModel> balanceList = ReportModelInit.getInstance().getPublicBalanceSheetFromModel();
				recentGeneralBalance = getEmptyReportData(report, balanceList,PublicBalanceSheet.REPORTNO);	
			}

			// 对需要展示的结果进行重排，小计项提前，小项前加两个空格
			recentGeneralBalance = resortReportDataOrder(recentGeneralBalance);
			recentGeneralMap.put("资产负债", recentGeneralBalance);
		}
		// 金融同业
		else if (recentList != null && recentList.size() > 0
				&& recentList.get(0).getId().getReportNo().startsWith("03")) {
			for (ReportData rd : recentList) {
				if (rd.getId().getReportNo().equals(FinanBalanceSheet.REPORTNO)
						&& rd.getDisplayFlag().substring(0, 1).equals("1"))
					recentGeneralBalance.add(rd);
				else if (rd.getId().getReportNo()
						.equals(FinanIncomeSheet.REPORTNO)
						&& rd.getDisplayFlag().substring(0, 1).equals("1"))
					recentGeneralIncome.add(rd);
				else {
					// log.info(arg0)
				}

			}
			
			//如果为空，从Model中去数据填充, 默认填充数据0.00
			if(recentGeneralBalance.size()<=0){
				List<ReportModel> balanceList = ReportModelInit.getInstance().getFinanBalanceSheetListFromModel();
				recentGeneralBalance = getEmptyReportData(report, balanceList,FinanBalanceSheet.REPORTNO);	
			}
			if(recentGeneralIncome.size() <= 0){
				List<ReportModel> incomeList = ReportModelInit.getInstance().getFinanIncomeFromModel();
				recentGeneralIncome = getEmptyReportData(report, incomeList,FinanIncomeSheet.REPORTNO);
			}
			
			recentGeneralBalance = resortReportDataOrder(recentGeneralBalance);
			recentGeneralIncome = resortReportDataOrder(recentGeneralIncome);
			
			recentGeneralMap.put("资产负债", recentGeneralBalance);
			recentGeneralMap.put("损益", recentGeneralIncome);
			model.addAttribute("recentGeneral", recentGeneralMap);

			return "finance/FA_GeneralStatement";

		}

		// 小企业
		else {
			for (ReportData rd : recentList) {
				if (rd.getId().getReportNo().equals(SmallBalanceSheet.REPORTNO)
						&& rd.getDisplayFlag().substring(0, 1).equals("1"))
					recentGeneralBalance.add(rd);
				else if (rd.getId().getReportNo()
						.equals(SmallIncomeSheet.REPORTNO)
						&& rd.getDisplayFlag().substring(0, 1).equals("1"))
					recentGeneralIncome.add(rd);
				else if (rd.getId().getReportNo()
						.equals(SmallCashFlowSheet.REPORTNO)
						&& rd.getDisplayFlag().substring(0, 1).equals("1"))
					recentGeneralCashFLow.add(rd);
				else {
					// log.info(arg0)
				}
			}

			//如果为空，从Model中去数据填充, 默认填充数据0.00
			if(recentGeneralBalance.size()<=0){
				List<ReportModel> balanceList = ReportModelInit.getInstance().getSmallBalanceSheetListFromModel();
				recentGeneralBalance = getEmptyReportData(report, balanceList,SmallBalanceSheet.REPORTNO);	
			}
			if(recentGeneralIncome.size() <= 0){
				List<ReportModel> incomeList = ReportModelInit.getInstance().getSmallIncomeFromModel();
				recentGeneralIncome = getEmptyReportData(report, incomeList,SmallIncomeSheet.REPORTNO);
			}
			if(recentGeneralCashFLow.size() <= 0 ){
				List<ReportModel> cashFlowList = ReportModelInit.getInstance().getSmallCashFlowFromModel();
				recentGeneralCashFLow = getEmptyReportData(report, cashFlowList,SmallCashFlowSheet.REPORTNO);
			}
			
			recentGeneralBalance = resortReportDataOrder(recentGeneralBalance);
			
			// 为小企业添加非流动资产合计这一项
			recentGeneralBalance = addNonAssetSum( report, recentList, recentGeneralBalance);
			
			recentGeneralMap.put("资产负债", recentGeneralBalance);
			recentGeneralMap.put("损益", recentGeneralIncome);
			recentGeneralMap.put("现金流量", recentGeneralCashFLow);
		}

		model.addAttribute("recentGeneral", recentGeneralMap);

		return "finance/FA_GeneralStatement";
	}


	private List<ReportData> addNonAssetSum(ReportList report, List<ReportData> reportDataList, List<ReportData> recentGeneralBalance) {
		Map<String,Double> dataMap = new LinkedHashMap<String,Double>();
		if(reportDataList!=null){
			for(ReportData  rd : reportDataList){
				dataMap.put(rd.getReportItemNo(), rd.getItemValue());
			}
		}		
		
		String displayNo = "38";
		String displayName = "非流动资产合计";
		String reportItemNo = "SBS051";
		
		Double itemValue = 0.00;
		Double SBS038 = dataMap.get("SBS038");
		Double SBS050 = dataMap.get("SBS050");
		Double SBS060 = dataMap.get("SBS060");
		if(SBS038==null) SBS038 = 0.00;
		if(SBS050==null) SBS050 = 0.00;
		if(SBS060==null) SBS060 = 0.00;
		
		itemValue = SBS038 + SBS050 + SBS060;
		
		ReportData rd = new ReportData();
		ReportDataId id = new ReportDataId();
		id.setCustId(report.getId().getCustId());
		id.setAccountMonth(report.getId().getAccountMonth());
		id.setReportNo(SmallBalanceSheet.REPORTNO);
		id.setReportScope(report.getId().getReportScope());
		id.setDisplayNo(displayNo);
		rd.setId(id);
		rd.setDisplayFlag("11");
		rd.setDisplayName(displayName);
		rd.setReportItemNo(reportItemNo);
		rd.setItemValue(itemValue);
		rd.setItemValueString(rd.getItemValueString());
		
		List<ReportData> dataList = new ArrayList<ReportData>();
		for(ReportData reportData : recentGeneralBalance){
			if(reportData.getReportItemNo().equals("SBS021")){
				dataList.add(rd);
			}

			dataList.add(reportData);
		}
		
		
		return dataList;
	}

	private List<ReportData> getEmptyReportData(ReportList report,
			List<ReportModel> reportModelList, String reportno) {
		List<ReportData> rdList = new ArrayList<ReportData>();
		for(ReportModel balance : reportModelList){
			if(balance.getDisplayFlag().substring(0, 1).equals("1")){
				String displayNo = ReportModelInit.getInstance().getDisplayNoById(balance.getFinanceItemNo());
				String displayFlag = ReportModelInit.getInstance().getDisplayFlagById(balance.getFinanceItemNo());
				ReportData rd = new ReportData();
				ReportDataId id = new ReportDataId();
				id.setCustId(report.getId().getCustId());
				id.setReportNo(reportno);
				id.setReportScope(report.getId().getReportScope());
				id.setAccountMonth(report.getId().getAccountMonth());
				id.setDisplayNo(displayNo);
				rd.setId(id);
				rd.setReportItemNo(balance.getFinanceItemNo());
				rd.setDisplayFlag(displayFlag);
				rd.setDisplayName(balance.getDisplayName());
				rd.setItemValue(0.00);
				rd.setItemValueString("0.00");	
				rdList.add(rd);
			}
		}
		return rdList;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/checkPreGeneralReport")
	public String checkPreGeneralReport(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		// 最近一期
		Map map = customerService.findRecentReport(request.getSession()
				.getAttribute("custId").toString());
		ReportList lastReport = null;
		ReportList lastOneYearReport = null;
		ReportList lastTwoYearReport = null;
		ReportList lastThreeYearReport = null;
		String lastDate = "";
		String lastOneDate = "";
		String lastTwoDate = "";
		String lastThreeDate = "";

		int i = 0;
		for (Object date : map.keySet()) {
			if (i == 0) {
				lastReport = (ReportList) map.get(date);
				lastDate = date.toString();
			} else if (i == 1) {
				lastOneYearReport = (ReportList) map.get(date);
				lastOneDate = date.toString();
			} else if (i == 2) {
				lastTwoYearReport = (ReportList) map.get(date);
				lastTwoDate = date.toString();
			} else if (i == 3) {
				lastThreeYearReport = (ReportList) map.get(date);
				lastThreeDate = date.toString();
			}
			i++;
		}

		if (lastReport != null) {
			List<ReportData> recentList = customerService
					.findRecentReportData(lastReport);
			// model.addAttribute("recentGeneral", recentList);
			Map<String, List<ReportData>> recentGeneralMap = new LinkedHashMap<String, List<ReportData>>();
			List<ReportData> recentGeneralBalance = new LinkedList<ReportData>();
			List<ReportData> recentGeneralIncome = new LinkedList<ReportData>();
			List<ReportData> recentGeneralCashFLow = new LinkedList<ReportData>();

			// 企业法人
			if (recentList != null && recentList.size() > 0
					&& recentList.get(0).getId().getReportNo().startsWith("01")) {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(EnterBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						recentGeneralBalance.add(rd);
					else if (rd.getId().getReportNo()
							.equals(EnterIncome.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						recentGeneralIncome.add(rd);
					else if (rd.getId().getReportNo()
							.equals(EnterCashFlow.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						recentGeneralCashFLow.add(rd);
					else {
						// log.info(arg0)
					}
				}
				
				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(recentGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getEnterBalanceSheetListFromModel();
					recentGeneralBalance = getEmptyReportData(lastReport, balanceList,EnterBalanceSheet.REPORTNO);	
				}
				if(recentGeneralIncome.size() <= 0){
					List<ReportModel> incomeList = ReportModelInit.getInstance().getEnterIncomeFromModel();
					recentGeneralIncome = getEmptyReportData(lastReport, incomeList,EnterIncome.REPORTNO);
				}
				if(recentGeneralCashFLow.size() <= 0 ){
					List<ReportModel> cashFlowList = ReportModelInit.getInstance().getEnterCashFlowFromModel();
					recentGeneralCashFLow = getEmptyReportData(lastReport, cashFlowList,EnterCashFlow.REPORTNO);
				}
				
				recentGeneralBalance = resortReportDataOrder(recentGeneralBalance);
				recentGeneralMap.put("资产负债", recentGeneralBalance);
				recentGeneralMap.put("损益", recentGeneralIncome);
				recentGeneralMap.put("现金流量", recentGeneralCashFLow);
			}
			// 事业单位
			else if (recentList != null && recentList.size() > 0
					&& recentList.get(0).getId().getReportNo().startsWith("02")) {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(PublicBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						recentGeneralBalance.add(rd);
					else {
						// log.info(arg0)
					}
				}
				
				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(recentGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getPublicBalanceSheetFromModel();
					recentGeneralBalance = getEmptyReportData(lastReport, balanceList,PublicBalanceSheet.REPORTNO);	
				}
				recentGeneralBalance = resortReportDataOrder(recentGeneralBalance);
				recentGeneralMap.put("资产负债", recentGeneralBalance);
			}
			// 金融同业
			else if (recentList != null && recentList.size() > 0
					&& recentList.get(0).getId().getReportNo().startsWith("03")) {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(FinanBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						recentGeneralBalance.add(rd);
					else if (rd.getId().getReportNo()
							.equals(FinanIncomeSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						recentGeneralIncome.add(rd);
					else {
						// log.info(arg0)
					}
				}
				
				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(recentGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getFinanBalanceSheetListFromModel();
					recentGeneralBalance = getEmptyReportData(lastReport, balanceList,FinanBalanceSheet.REPORTNO);	
				}
				if(recentGeneralIncome.size() <= 0){
					List<ReportModel> incomeList = ReportModelInit.getInstance().getFinanIncomeFromModel();
					recentGeneralIncome = getEmptyReportData(lastReport, incomeList,FinanIncomeSheet.REPORTNO);
				}
				recentGeneralBalance = resortReportDataOrder(recentGeneralBalance);
				recentGeneralIncome = resortReportDataOrder(recentGeneralIncome);
				recentGeneralMap.put("资产负债", recentGeneralBalance);
				recentGeneralMap.put("损益", recentGeneralIncome);

			} else {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(SmallBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						recentGeneralBalance.add(rd);
					else if (rd.getId().getReportNo()
							.equals(SmallIncomeSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						recentGeneralIncome.add(rd);
					else if (rd.getId().getReportNo()
							.equals(SmallCashFlowSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						recentGeneralCashFLow.add(rd);
					else {
						// log.info(arg0)
					}
				}
				

				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(recentGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getSmallBalanceSheetListFromModel();
					recentGeneralBalance = getEmptyReportData(lastReport, balanceList,SmallBalanceSheet.REPORTNO);	
				}
				if(recentGeneralIncome.size() <= 0){
					List<ReportModel> incomeList = ReportModelInit.getInstance().getSmallIncomeFromModel();
					recentGeneralIncome = getEmptyReportData(lastReport, incomeList,SmallIncomeSheet.REPORTNO);
				}
				if(recentGeneralCashFLow.size() <= 0 ){
					List<ReportModel> cashFlowList = ReportModelInit.getInstance().getSmallCashFlowFromModel();
					recentGeneralCashFLow = getEmptyReportData(lastReport, cashFlowList,SmallCashFlowSheet.REPORTNO);
				}
				
				recentGeneralBalance = resortReportDataOrder(recentGeneralBalance);
				
				// 为小企业添加非流动资产合计这一项
				recentGeneralBalance = addNonAssetSum( lastReport, recentList, recentGeneralBalance);
				
				recentGeneralMap.put("资产负债", recentGeneralBalance);
				recentGeneralMap.put("损益", recentGeneralIncome);
				recentGeneralMap.put("现金流量", recentGeneralCashFLow);
			}

			model.addAttribute("recentGeneral", recentGeneralMap);

		}
		// 前一年
		if (lastOneYearReport != null) {
			ReportList recentReport = lastOneYearReport;
			List<ReportData> recentList = customerService
					.findRecentReportData(recentReport);

			Map<String, List<ReportData>> lastOneGeneralMap = new LinkedHashMap<String, List<ReportData>>();
			List<ReportData> lastOneGeneralBalance = new LinkedList<ReportData>();
			List<ReportData> lastOneGeneralIncome = new LinkedList<ReportData>();
			List<ReportData> lastOneGeneralCashFLow = new LinkedList<ReportData>();

			// 企业法人
			if (recentList != null && recentList.size() > 0
					&& recentList.get(0).getId().getReportNo().startsWith("01")) {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(EnterBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastOneGeneralBalance.add(rd);
					else if (rd.getId().getReportNo()
							.equals(EnterIncome.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastOneGeneralIncome.add(rd);
					else if (rd.getId().getReportNo()
							.equals(EnterCashFlow.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastOneGeneralCashFLow.add(rd);
					else {
						// log.info(arg0)
					}
				}
					//如果为空，从Model中去数据填充, 默认填充数据0.00
					if(lastOneGeneralBalance.size()<=0){
						List<ReportModel> balanceList = ReportModelInit.getInstance().getEnterBalanceSheetListFromModel();
						lastOneGeneralBalance = getEmptyReportData(lastOneYearReport, balanceList,EnterBalanceSheet.REPORTNO);	
					}
					if(lastOneGeneralIncome.size() <= 0){
						List<ReportModel> incomeList = ReportModelInit.getInstance().getEnterIncomeFromModel();
						lastOneGeneralIncome = getEmptyReportData(lastOneYearReport, incomeList,EnterIncome.REPORTNO);
					}
					if(lastOneGeneralCashFLow.size() <= 0 ){
						List<ReportModel> cashFlowList = ReportModelInit.getInstance().getEnterCashFlowFromModel();
						lastOneGeneralCashFLow = getEmptyReportData(lastOneYearReport, cashFlowList,EnterCashFlow.REPORTNO);
					}
					
					lastOneGeneralBalance = resortReportDataOrder(lastOneGeneralBalance);
					lastOneGeneralMap.put("资产负债", lastOneGeneralBalance);
					lastOneGeneralMap.put("损益", lastOneGeneralIncome);
					lastOneGeneralMap.put("现金流量", lastOneGeneralCashFLow);

				
			}// 事业单位
			else if (recentList != null && recentList.size() > 0
					&& recentList.get(0).getId().getReportNo().startsWith("02")) {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(PublicBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastOneGeneralBalance.add(rd);
					else {
						// log.info(arg0)
					}
				}
				
				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(lastOneGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getPublicBalanceSheetFromModel();
					lastOneGeneralBalance = getEmptyReportData(lastOneYearReport, balanceList,PublicBalanceSheet.REPORTNO);	
				}
				
				lastOneGeneralBalance = resortReportDataOrder(lastOneGeneralBalance);
				lastOneGeneralMap.put("资产负债", lastOneGeneralBalance);
			}
			// 金融同业
			else if (recentList != null && recentList.size() > 0
					&& recentList.get(0).getId().getReportNo().startsWith("03")) {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(FinanBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastOneGeneralBalance.add(rd);
					else if (rd.getId().getReportNo()
							.equals(FinanIncomeSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastOneGeneralIncome.add(rd);
					else {
						// log.info(arg0)
					}

				}
				
				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(lastOneGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getFinanBalanceSheetListFromModel();
					lastOneGeneralBalance = getEmptyReportData(lastOneYearReport, balanceList,FinanBalanceSheet.REPORTNO);	
				}
				if(lastOneGeneralIncome.size() <= 0){
					List<ReportModel> incomeList = ReportModelInit.getInstance().getFinanIncomeFromModel();
					lastOneGeneralIncome = getEmptyReportData(lastOneYearReport, incomeList,FinanIncomeSheet.REPORTNO);
				}
				
				lastOneGeneralBalance = resortReportDataOrder(lastOneGeneralBalance);
				lastOneGeneralIncome = resortReportDataOrder(lastOneGeneralIncome);
				lastOneGeneralMap.put("资产负债", lastOneGeneralBalance);
				lastOneGeneralMap.put("损益", lastOneGeneralIncome);

			} else {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(SmallBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastOneGeneralBalance.add(rd);
					else if (rd.getId().getReportNo()
							.equals(SmallIncomeSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastOneGeneralIncome.add(rd);
					else if (rd.getId().getReportNo()
							.equals(SmallCashFlowSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastOneGeneralCashFLow.add(rd);
					else {
						// log.info(arg0)
					}
				}
				
				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(lastOneGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getSmallBalanceSheetListFromModel();
					lastOneGeneralBalance = getEmptyReportData(lastOneYearReport, balanceList,SmallBalanceSheet.REPORTNO);	
				}
				if(lastOneGeneralIncome.size() <= 0){
					List<ReportModel> incomeList = ReportModelInit.getInstance().getSmallIncomeFromModel();
					lastOneGeneralIncome = getEmptyReportData(lastOneYearReport, incomeList,SmallIncomeSheet.REPORTNO);
				}
				if(lastOneGeneralCashFLow.size() <= 0 ){
					List<ReportModel> cashFlowList = ReportModelInit.getInstance().getSmallCashFlowFromModel();
					lastOneGeneralCashFLow = getEmptyReportData(lastOneYearReport, cashFlowList,SmallCashFlowSheet.REPORTNO);
				}
				
				lastOneGeneralBalance = resortReportDataOrder(lastOneGeneralBalance);
				
				// 为小企业添加非流动资产合计这一项
				lastOneGeneralBalance = addNonAssetSum( lastOneYearReport, recentList, lastOneGeneralBalance);
				
				lastOneGeneralMap.put("资产负债", lastOneGeneralBalance);
				lastOneGeneralMap.put("损益", lastOneGeneralIncome);
				lastOneGeneralMap.put("现金流量", lastOneGeneralCashFLow);
			}

			model.addAttribute("lastOneGeneral", lastOneGeneralMap);

		}
		// 前两年
		if (lastTwoYearReport != null) {
			ReportList recentReport = lastTwoYearReport;

			List<ReportData> recentList = customerService
					.findRecentReportData(recentReport);
			model.addAttribute("lastTwoGeneral", recentList);

			Map<String, List<ReportData>> lastTwoGeneralMap = new LinkedHashMap<String, List<ReportData>>();
			List<ReportData> lastTwoGeneralBalance = new LinkedList<ReportData>();
			List<ReportData> lastTwoGeneralIncome = new LinkedList<ReportData>();
			List<ReportData> lastTwoGeneralCashFLow = new LinkedList<ReportData>();

			// 企业法人
			if (recentList != null && recentList.size() > 0
					&& recentList.get(0).getId().getReportNo().startsWith("01")) {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(EnterBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastTwoGeneralBalance.add(rd);
					else if (rd.getId().getReportNo()
							.equals(EnterIncome.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastTwoGeneralIncome.add(rd);
					else if (rd.getId().getReportNo()
							.equals(EnterCashFlow.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastTwoGeneralCashFLow.add(rd);
					else {
						// log.info(arg0)
					}
				}	
					//如果为空，从Model中去数据填充, 默认填充数据0.00
					if(lastTwoGeneralBalance.size()<=0){
						List<ReportModel> balanceList = ReportModelInit.getInstance().getEnterBalanceSheetListFromModel();
						lastTwoGeneralBalance = getEmptyReportData(lastTwoYearReport, balanceList,EnterBalanceSheet.REPORTNO);	
					}
					if(lastTwoGeneralIncome.size() <= 0){
						List<ReportModel> incomeList = ReportModelInit.getInstance().getEnterIncomeFromModel();
						lastTwoGeneralIncome = getEmptyReportData(lastTwoYearReport, incomeList,EnterIncome.REPORTNO);
					}
					if(lastTwoGeneralCashFLow.size() <= 0 ){
						List<ReportModel> cashFlowList = ReportModelInit.getInstance().getEnterCashFlowFromModel();
						lastTwoGeneralCashFLow = getEmptyReportData(lastTwoYearReport, cashFlowList,EnterCashFlow.REPORTNO);
					}
					
					lastTwoGeneralBalance = resortReportDataOrder(lastTwoGeneralBalance);
					lastTwoGeneralMap.put("资产负债", lastTwoGeneralBalance);
					lastTwoGeneralMap.put("损益", lastTwoGeneralIncome);
					lastTwoGeneralMap.put("现金流量", lastTwoGeneralCashFLow);

				
			}// 事业单位
			else if (recentList != null && recentList.size() > 0
					&& recentList.get(0).getId().getReportNo().startsWith("02")) {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(PublicBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastTwoGeneralBalance.add(rd);
					else {
						// log.info(arg0)
					}
				}
				
				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(lastTwoGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getPublicBalanceSheetFromModel();
					lastTwoGeneralBalance = getEmptyReportData(lastTwoYearReport, balanceList,PublicBalanceSheet.REPORTNO);	
				}
				
				lastTwoGeneralBalance = resortReportDataOrder(lastTwoGeneralBalance);
				lastTwoGeneralMap.put("资产负债", lastTwoGeneralBalance);
			}
			// 金融同业
			else if (recentList != null && recentList.size() > 0
					&& recentList.get(0).getId().getReportNo().startsWith("03")) {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(FinanBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastTwoGeneralBalance.add(rd);
					else if (rd.getId().getReportNo()
							.equals(FinanIncomeSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastTwoGeneralIncome.add(rd);
					else {
						// log.info(arg0)
					}

				}
				
				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(lastTwoGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getFinanBalanceSheetListFromModel();
					lastTwoGeneralBalance = getEmptyReportData(lastTwoYearReport, balanceList,FinanBalanceSheet.REPORTNO);	
				}
				if(lastTwoGeneralIncome.size() <= 0){
					List<ReportModel> incomeList = ReportModelInit.getInstance().getFinanIncomeFromModel();
					lastTwoGeneralIncome = getEmptyReportData(lastTwoYearReport, incomeList,FinanIncomeSheet.REPORTNO);
				}
				
				lastTwoGeneralMap.put("资产负债", lastTwoGeneralBalance);
				lastTwoGeneralMap.put("损益", lastTwoGeneralIncome);

			} else {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(SmallBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastTwoGeneralBalance.add(rd);
					else if (rd.getId().getReportNo()
							.equals(SmallIncomeSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastTwoGeneralIncome.add(rd);
					else if (rd.getId().getReportNo()
							.equals(SmallCashFlowSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastTwoGeneralCashFLow.add(rd);
					else {
						// log.info(arg0)
					}
				}
				
				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(lastTwoGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getSmallBalanceSheetListFromModel();
					lastTwoGeneralBalance = getEmptyReportData(lastTwoYearReport, balanceList,SmallBalanceSheet.REPORTNO);	
				}
				if(lastTwoGeneralIncome.size() <= 0){
					List<ReportModel> incomeList = ReportModelInit.getInstance().getSmallIncomeFromModel();
					lastTwoGeneralIncome = getEmptyReportData(lastTwoYearReport, incomeList,SmallIncomeSheet.REPORTNO);
				}
				if(lastTwoGeneralCashFLow.size() <= 0 ){
					List<ReportModel> cashFlowList = ReportModelInit.getInstance().getSmallCashFlowFromModel();
					lastTwoGeneralCashFLow = getEmptyReportData(lastTwoYearReport, cashFlowList,SmallCashFlowSheet.REPORTNO);
				}
				
				lastTwoGeneralBalance = resortReportDataOrder(lastTwoGeneralBalance);
				
				// 为小企业添加非流动资产合计这一项
				lastTwoGeneralBalance = addNonAssetSum( lastTwoYearReport, recentList, lastTwoGeneralBalance);
				
				lastTwoGeneralMap.put("资产负债", lastTwoGeneralBalance);
				lastTwoGeneralMap.put("损益", lastTwoGeneralIncome);
				lastTwoGeneralMap.put("现金流量", lastTwoGeneralCashFLow);
			}

			model.addAttribute("lastTwoGeneral", lastTwoGeneralMap);

		}

		// 前三年
		if (lastThreeYearReport != null) {
			ReportList recentReport = lastThreeYearReport;

			List<ReportData> recentList = customerService
					.findRecentReportData(recentReport);
			model.addAttribute("lastThreeGeneral", recentList);

			Map<String, List<ReportData>> lastThreeGeneralMap = new LinkedHashMap<String, List<ReportData>>();
			List<ReportData> lastThreeGeneralBalance = new LinkedList<ReportData>();
			List<ReportData> lastThreeGeneralIncome = new LinkedList<ReportData>();
			List<ReportData> lastThreeGeneralCashFLow = new LinkedList<ReportData>();

			// 企业法人
			if (recentList != null && recentList.size() > 0
					&& recentList.get(0).getId().getReportNo().startsWith("01")) {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(EnterBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastThreeGeneralBalance.add(rd);
					else if (rd.getId().getReportNo()
							.equals(EnterIncome.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastThreeGeneralIncome.add(rd);
					else if (rd.getId().getReportNo()
							.equals(EnterCashFlow.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastThreeGeneralCashFLow.add(rd);
					else {
						// log.info(arg0)
					}
				}
					//如果为空，从Model中去数据填充, 默认填充数据0.00
					if(lastThreeGeneralBalance.size()<=0){
						List<ReportModel> balanceList = ReportModelInit.getInstance().getEnterBalanceSheetListFromModel();
						lastThreeGeneralBalance = getEmptyReportData(lastThreeYearReport, balanceList,EnterBalanceSheet.REPORTNO);	
					}
					if(lastThreeGeneralIncome.size() <= 0){
						List<ReportModel> incomeList = ReportModelInit.getInstance().getEnterIncomeFromModel();
						lastThreeGeneralIncome = getEmptyReportData(lastThreeYearReport, incomeList,EnterIncome.REPORTNO);
					}
					if(lastThreeGeneralCashFLow.size() <= 0 ){
						List<ReportModel> cashFlowList = ReportModelInit.getInstance().getEnterCashFlowFromModel();
						lastThreeGeneralCashFLow = getEmptyReportData(lastThreeYearReport, cashFlowList,EnterCashFlow.REPORTNO);
					}
					
					lastThreeGeneralBalance = resortReportDataOrder(lastThreeGeneralBalance);
					lastThreeGeneralMap.put("资产负债", lastThreeGeneralBalance);
					lastThreeGeneralMap.put("损益", lastThreeGeneralIncome);
					lastThreeGeneralMap.put("现金流量", lastThreeGeneralCashFLow);

				

			}// 事业单位
			else if (recentList != null && recentList.size() > 0
					&& recentList.get(0).getId().getReportNo().startsWith("02")) {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(PublicBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastThreeGeneralBalance.add(rd);
					else {
						// log.info(arg0)
					}
				}
				
				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(lastThreeGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getPublicBalanceSheetFromModel();
					lastThreeGeneralBalance = getEmptyReportData(lastThreeYearReport, balanceList,PublicBalanceSheet.REPORTNO);	
				}
				
				lastThreeGeneralBalance = resortReportDataOrder(lastThreeGeneralBalance);
				lastThreeGeneralMap.put("资产负债", lastThreeGeneralBalance);
			}
			// 金融同业
			else if (recentList != null && recentList.size() > 0
					&& recentList.get(0).getId().getReportNo().startsWith("03")) {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(FinanBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastThreeGeneralBalance.add(rd);
					else if (rd.getId().getReportNo()
							.equals(FinanIncomeSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastThreeGeneralIncome.add(rd);
					else {
						// log.info(arg0)
					}
				}
				
				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(lastThreeGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getFinanBalanceSheetListFromModel();
					lastThreeGeneralBalance = getEmptyReportData(lastThreeYearReport, balanceList,FinanBalanceSheet.REPORTNO);	
				}
				if(lastThreeGeneralIncome.size() <= 0){
					List<ReportModel> incomeList = ReportModelInit.getInstance().getFinanIncomeFromModel();
					lastThreeGeneralIncome = getEmptyReportData(lastThreeYearReport, incomeList,FinanIncomeSheet.REPORTNO);
				}
				
				lastThreeGeneralMap.put("资产负债", lastThreeGeneralBalance);
				lastThreeGeneralMap.put("损益", lastThreeGeneralIncome);

			} else {
				for (ReportData rd : recentList) {
					if (rd.getId().getReportNo()
							.equals(SmallBalanceSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastThreeGeneralBalance.add(rd);
					else if (rd.getId().getReportNo()
							.equals(SmallIncomeSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastThreeGeneralIncome.add(rd);
					else if (rd.getId().getReportNo()
							.equals(SmallCashFlowSheet.REPORTNO)
							&& rd.getDisplayFlag().substring(0, 1).equals("1"))
						lastThreeGeneralCashFLow.add(rd);
					else {
						// log.info(arg0)
					}
				}
				

				//如果为空，从Model中去数据填充, 默认填充数据0.00
				if(lastThreeGeneralBalance.size()<=0){
					List<ReportModel> balanceList = ReportModelInit.getInstance().getSmallBalanceSheetListFromModel();
					lastThreeGeneralBalance = getEmptyReportData(lastThreeYearReport, balanceList,SmallBalanceSheet.REPORTNO);	
				}
				if(lastThreeGeneralIncome.size() <= 0){
					List<ReportModel> incomeList = ReportModelInit.getInstance().getSmallIncomeFromModel();
					lastThreeGeneralIncome = getEmptyReportData(lastThreeYearReport, incomeList,SmallIncomeSheet.REPORTNO);
				}
				if(lastThreeGeneralCashFLow.size() <= 0 ){
					List<ReportModel> cashFlowList = ReportModelInit.getInstance().getSmallCashFlowFromModel();
					lastThreeGeneralCashFLow = getEmptyReportData(lastThreeYearReport, cashFlowList,SmallCashFlowSheet.REPORTNO);
				}
				
				lastThreeGeneralBalance = resortReportDataOrder(lastThreeGeneralBalance);
				
				// 为小企业添加非流动资产合计这一项
				lastThreeGeneralBalance = addNonAssetSum( lastThreeYearReport, recentList, lastThreeGeneralBalance);
				
				lastThreeGeneralMap.put("资产负债", lastThreeGeneralBalance);
				lastThreeGeneralMap.put("损益", lastThreeGeneralIncome);
				lastThreeGeneralMap.put("现金流量", lastThreeGeneralCashFLow);
			}

			model.addAttribute("lastThreeGeneral", lastThreeGeneralMap);
		}

		model.addAttribute("lastDate", lastDate);
		model.addAttribute("lastOneDate", lastOneDate);
		model.addAttribute("lastTwoDate", lastTwoDate);
		model.addAttribute("lastThreeDate", lastThreeDate);

		return "finance/GeneralStatement_Trend";
	}

	// 查看当前财务指标表
	@RequestMapping("/checkRecentFinancialIndex")
	public String checkRecentFinancialIndex(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		ReportList report = new ReportList();

		CustInfo currentCust = new CustInfo();

		report = (ReportList) request.getSession().getAttribute("report");
		currentCust = customerService.findCustByCustId(report.getId()
				.getCustId());
		Map<String, List<ReportData>> recentFIMap = new LinkedHashMap<String, List<ReportData>>();

		if (currentCust.getCustType().equals("01")) {
			List<ReportData> recentFIList;
			if (report.getReportType().equals("02")) {
				recentFIList = customerService.findRecentReportData(report,
						"0505");
			} else {
				recentFIList = customerService.findRecentReportData(report,
						"0105");
			}
			List<ReportData> list1 = new LinkedList<ReportData>();
			List<ReportData> list2 = new LinkedList<ReportData>();
			List<ReportData> list3 = new LinkedList<ReportData>();
			List<ReportData> list4 = new LinkedList<ReportData>();
			List<ReportData> list5 = new LinkedList<ReportData>();
			List<ReportData> list6 = new LinkedList<ReportData>();
			List<ReportData> list7 = new LinkedList<ReportData>();
			List<ReportData> list8 = new LinkedList<ReportData>();

			for (ReportData rd : recentFIList) {
				if (rd.getDisplayFlag().equals("1")) {
					list1.add(rd);
				} else if (rd.getDisplayFlag().equals("2")) {
					list2.add(rd);
				} else if (rd.getDisplayFlag().equals("3")) {
					list3.add(rd);
				} else if (rd.getDisplayFlag().equals("4")) {
					list4.add(rd);
				} else if (rd.getDisplayFlag().equals("5")) {
					list5.add(rd);
				} else if (rd.getDisplayFlag().equals("6")) {
					list6.add(rd);
				} else if (rd.getDisplayFlag().equals("7")) {
					list7.add(rd);
				} else if (rd.getDisplayFlag().equals("8")) {
					list8.add(rd);
				}
			}

			recentFIMap.put("短期偿债能力", list1);
			recentFIMap.put("长期偿债能力", list2);
			recentFIMap.put("营运能力", list3);
			recentFIMap.put("盈利能力", list4);
			recentFIMap.put("流动性", list5);
			recentFIMap.put("规模指标", list6);
			recentFIMap.put("财务结构", list7);
			recentFIMap.put("成长性", list8);

			model.addAttribute("recentFinancialIndex", recentFIMap);

		}
		// 事业单位
		else if (currentCust.getCustType().equals("02")) {
			List<ReportData> recentFIList = customerService
					.findRecentReportData(report, PublicFinancialIndex.REPORTNO);

			recentFIMap.put("事业单位财务指标", recentFIList);
			model.addAttribute("recentFinancialIndex", recentFIMap);
		}
		// 金融同业
		else if (currentCust.getCustType().equals("03")) {
			List<ReportData> recentFIList = customerService
					.findRecentReportData(report, "0303");
			List<ReportData> list1 = new LinkedList<ReportData>();
			List<ReportData> list2 = new LinkedList<ReportData>();
			List<ReportData> list3 = new LinkedList<ReportData>();
			List<ReportData> list4 = new LinkedList<ReportData>();
			List<ReportData> list5 = new LinkedList<ReportData>();
			List<ReportData> list6 = new LinkedList<ReportData>();

			for (ReportData rd : recentFIList) {
				if (rd.getDisplayFlag().length() == 2
						&& rd.getDisplayFlag().substring(0, 1).equals("1")) {
					list1.add(rd);
				} else if (rd.getDisplayFlag().length() == 2
						&& rd.getDisplayFlag().substring(0, 1).equals("2")) {
					list2.add(rd);
				} else if (rd.getDisplayFlag().length() == 2
						&& rd.getDisplayFlag().substring(0, 1).equals("3")) {
					list3.add(rd);
				} else if (rd.getDisplayFlag().length() == 2
						&& rd.getDisplayFlag().substring(0, 1).equals("4")) {
					list4.add(rd);
				} else if (rd.getDisplayFlag().length() == 2
						&& rd.getDisplayFlag().substring(0, 1).equals("5")) {
					list5.add(rd);
				} else if (rd.getDisplayFlag().length() == 2
						&& rd.getDisplayFlag().substring(0, 1).equals("6")) {
					list6.add(rd);
				}
			}

			recentFIMap.put("资产质量", list1);
			recentFIMap.put("盈利能力", list2);
			recentFIMap.put("资本充足率", list3);
			recentFIMap.put("流动性", list4);
			recentFIMap.put("规模", list5);
			recentFIMap.put("成长性", list6);

			model.addAttribute("recentFinancialIndex", recentFIMap);
		}
		// 小企业
		else {

		}
		/*
		 * currentCust = customerService.findCustByCustId(custId);
		 * currentCust.setCustType
		 * (DataDictionary.getInstance().getNameByCodeAndId("CustType",
		 * currentCust.getCustType())); model.addAttribute("CurrentCust",
		 * currentCust);
		 */

		return "finance/FA_FinancialIndex";
	}

	@RequestMapping("/checkPreFinancialIndex")
	public String checkPreFinancialIndex(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		// 最近一期
		Map map = customerService.findRecentReport(request.getSession()
				.getAttribute("custId").toString());
		CustInfo currentCust = customerService.findCustByCustId(request
				.getSession().getAttribute("custId").toString());

		ReportList lastReport = null;
		ReportList lastOneYearReport = null;
		ReportList lastTwoYearReport = null;
		ReportList lastThreeYearReport = null;
		String lastDate = "";
		String lastOneDate = "";
		String lastTwoDate = "";
		String lastThreeDate = "";

		int i = 0;
		for (Object date : map.keySet()) {
			if (i == 0) {
				lastReport = (ReportList) map.get(date);
				lastDate = date.toString();
			} else if (i == 1) {
				lastOneYearReport = (ReportList) map.get(date);
				lastOneDate = date.toString();
			} else if (i == 2) {
				lastTwoYearReport = (ReportList) map.get(date);
				lastTwoDate = date.toString();
			} else if (i == 3) {
				lastThreeYearReport = (ReportList) map.get(date);
				lastThreeDate = date.toString();
			}
			i++;
		}

		// 加载报表项
		Map<String, List<ReportModel>> fIItemMap = new LinkedHashMap<String, List<ReportModel>>();
		List<ReportModel> fIList = new ArrayList<ReportModel>();
		// 企业法人
		if (currentCust.getCustType().equals("01")) {
			fIList = ReportModelInit.getInstance()
					.getEnterFinancialIndexFromModel();

			List<ReportModel> itemList1 = new ArrayList();
			List<ReportModel> itemList2 = new ArrayList();
			List<ReportModel> itemList3 = new ArrayList();
			List<ReportModel> itemList4 = new ArrayList();
			List<ReportModel> itemList5 = new ArrayList();
			List<ReportModel> itemList6 = new ArrayList();
			List<ReportModel> itemList7 = new ArrayList();
			List<ReportModel> itemList8 = new ArrayList();

			for (ReportModel rm : fIList) {
				if (rm.getDisplayFlag().equals("1")) {
					itemList1.add(rm);
				} else if (rm.getDisplayFlag().equals("2")) {
					itemList2.add(rm);
				} else if (rm.getDisplayFlag().equals("3")) {
					itemList3.add(rm);
				} else if (rm.getDisplayFlag().equals("4")) {
					itemList4.add(rm);
				} else if (rm.getDisplayFlag().equals("5")) {
					itemList5.add(rm);
				} else if (rm.getDisplayFlag().equals("6")) {
					itemList6.add(rm);
				} else if (rm.getDisplayFlag().equals("7")) {
					itemList7.add(rm);
				} else if (rm.getDisplayFlag().equals("8")) {
					itemList8.add(rm);
				}
			}

			fIItemMap.put("短期偿债能力", itemList1);
			fIItemMap.put("长期偿债能力", itemList2);
			fIItemMap.put("营运能力", itemList3);
			fIItemMap.put("盈利能力", itemList4);
			fIItemMap.put("流动性", itemList5);
			fIItemMap.put("规模指标", itemList6);
			fIItemMap.put("财务结构", itemList7);
			fIItemMap.put("成长性", itemList8);
		}
		// 事业单位
		else if (currentCust.getCustType().equals("02")) {
			fIList = ReportModelInit.getInstance()
					.getPublicFinancialIndexFromModel();

			fIItemMap.put("事业单位财务指标", fIList);
		}
		// TODO 金融同业 刘鹏你加上
		else if (currentCust.getCustType().equals("03")) {
			fIList = ReportModelInit.getInstance()
					.getFinanFinancialIndexFromModel();

			List<ReportModel> itemList1 = new ArrayList();
			List<ReportModel> itemList2 = new ArrayList();
			List<ReportModel> itemList3 = new ArrayList();
			List<ReportModel> itemList4 = new ArrayList();
			List<ReportModel> itemList5 = new ArrayList();
			List<ReportModel> itemList6 = new ArrayList();

			for (ReportModel rm : fIList) {
				if (rm.getDisplayFlag().substring(0, 1).equals("1")) {
					itemList1.add(rm);
				} else if (rm.getDisplayFlag().substring(0, 1).equals("2")) {
					itemList2.add(rm);
				} else if (rm.getDisplayFlag().substring(0, 1).equals("3")) {
					itemList3.add(rm);
				} else if (rm.getDisplayFlag().substring(0, 1).equals("4")) {
					itemList4.add(rm);
				} else if (rm.getDisplayFlag().substring(0, 1).equals("5")) {
					itemList5.add(rm);
				} else if (rm.getDisplayFlag().substring(0, 1).equals("6")) {
					itemList6.add(rm);
				}
			}

			fIItemMap.put("资产质量", itemList1);
			fIItemMap.put("盈利能力", itemList2);
			fIItemMap.put("资本充足率", itemList3);
			fIItemMap.put("流动性", itemList4);
			fIItemMap.put("规模", itemList5);
			fIItemMap.put("成长性", itemList6);

		} else {

		}

		model.addAttribute("financialIndexItem", fIItemMap);

		if (lastReport != null) {
			// 企业法人
			Map<String, List<ReportData>> recentFIMap = new LinkedHashMap<String, List<ReportData>>();

			if (currentCust.getCustType().equals("01")) {
				List<ReportData> recentFIList;
				if (lastReport.getReportType().equals("02")) {
					recentFIList = customerService.findRecentReportData(
							lastReport, "0505");
				} else {
					recentFIList = customerService.findRecentReportData(
							lastReport, "0105");
				}
				List<ReportData> list1 = new LinkedList<ReportData>();
				List<ReportData> list2 = new LinkedList<ReportData>();
				List<ReportData> list3 = new LinkedList<ReportData>();
				List<ReportData> list4 = new LinkedList<ReportData>();
				List<ReportData> list5 = new LinkedList<ReportData>();
				List<ReportData> list6 = new LinkedList<ReportData>();
				List<ReportData> list7 = new LinkedList<ReportData>();
				List<ReportData> list8 = new LinkedList<ReportData>();

				for (ReportData rd : recentFIList) {
					if (rd.getDisplayFlag().equals("1")) {
						list1.add(rd);
					} else if (rd.getDisplayFlag().equals("2")) {
						list2.add(rd);
					} else if (rd.getDisplayFlag().equals("3")) {
						list3.add(rd);
					} else if (rd.getDisplayFlag().equals("4")) {
						list4.add(rd);
					} else if (rd.getDisplayFlag().equals("5")) {
						list5.add(rd);
					} else if (rd.getDisplayFlag().equals("6")) {
						list6.add(rd);
					} else if (rd.getDisplayFlag().equals("7")) {
						list7.add(rd);
					} else if (rd.getDisplayFlag().equals("8")) {
						list8.add(rd);
					}
				}

				recentFIMap.put("短期偿债能力", list1);
				recentFIMap.put("长期偿债能力", list2);
				recentFIMap.put("营运能力", list3);
				recentFIMap.put("盈利能力", list4);
				recentFIMap.put("流动性", list5);
				recentFIMap.put("规模指标", list6);
				recentFIMap.put("财务结构", list7);
				recentFIMap.put("成长性", list8);
			}
			// 事业单位
			else if (currentCust.getCustType().equals("02")) {
				List<ReportData> recentFIList = customerService
						.findRecentReportData(lastReport,
								PublicFinancialIndex.REPORTNO);
				recentFIMap.put("事业单位财务指标", recentFIList);
			}
			// TODO 金融同业 刘鹏你来添加
			else if (currentCust.getCustType().equals("03")) {
				List<ReportData> recentFIList = customerService
						.findRecentReportData(lastReport,
								FinanFinancialIndexSheet.REPORTNO);

				List<ReportData> list1 = new LinkedList<ReportData>();
				List<ReportData> list2 = new LinkedList<ReportData>();
				List<ReportData> list3 = new LinkedList<ReportData>();
				List<ReportData> list4 = new LinkedList<ReportData>();
				List<ReportData> list5 = new LinkedList<ReportData>();
				List<ReportData> list6 = new LinkedList<ReportData>();

				for (ReportData rd : recentFIList) {
					if (rd.getDisplayFlag().substring(0, 1).equals("1")) {
						list1.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("2")) {
						list2.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("3")) {
						list3.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("4")) {
						list4.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("5")) {
						list5.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("6")) {
						list6.add(rd);
					}
				}

				recentFIMap.put("资产质量", list1);
				recentFIMap.put("盈利能力", list2);
				recentFIMap.put("资本充足率", list3);
				recentFIMap.put("流动性", list4);
				recentFIMap.put("规模", list5);
				recentFIMap.put("成长性", list6);

			} else {

			}

			model.addAttribute("recentFinancialIndex", recentFIMap);

		}
		// 前一年
		if (lastOneYearReport != null) {
			// 企业法人
			Map<String, List<ReportData>> recentFIMap = new LinkedHashMap<String, List<ReportData>>();

			if (currentCust.getCustType().equals("01")) {
				List<ReportData> recentFIList;
				if (lastOneYearReport.getReportType().equals("02")) {
					recentFIList = customerService.findRecentReportData(
							lastOneYearReport, "0505");
				} else {
					recentFIList = customerService.findRecentReportData(
							lastOneYearReport, "0105");
				}
				List<ReportData> list1 = new LinkedList<ReportData>();
				List<ReportData> list2 = new LinkedList<ReportData>();
				List<ReportData> list3 = new LinkedList<ReportData>();
				List<ReportData> list4 = new LinkedList<ReportData>();
				List<ReportData> list5 = new LinkedList<ReportData>();
				List<ReportData> list6 = new LinkedList<ReportData>();
				List<ReportData> list7 = new LinkedList<ReportData>();
				List<ReportData> list8 = new LinkedList<ReportData>();

				for (ReportData rd : recentFIList) {
					if (rd.getDisplayFlag().equals("1")) {
						list1.add(rd);
					} else if (rd.getDisplayFlag().equals("2")) {
						list2.add(rd);
					} else if (rd.getDisplayFlag().equals("3")) {
						list3.add(rd);
					} else if (rd.getDisplayFlag().equals("4")) {
						list4.add(rd);
					} else if (rd.getDisplayFlag().equals("5")) {
						list5.add(rd);
					} else if (rd.getDisplayFlag().equals("6")) {
						list6.add(rd);
					} else if (rd.getDisplayFlag().equals("7")) {
						list7.add(rd);
					} else if (rd.getDisplayFlag().equals("8")) {
						list8.add(rd);
					}

				}

				recentFIMap.put("短期偿债能力", list1);
				recentFIMap.put("长期偿债能力", list2);
				recentFIMap.put("营运能力", list3);
				recentFIMap.put("盈利能力", list4);
				recentFIMap.put("流动性", list5);
				recentFIMap.put("规模指标", list6);
				recentFIMap.put("财务结构", list7);
				recentFIMap.put("成长性", list8);
			}
			// 事业单位
			else if (currentCust.getCustType().equals("02")) {
				List<ReportData> recentFIList = customerService
						.findRecentReportData(lastOneYearReport,
								PublicFinancialIndex.REPORTNO);
				recentFIMap.put("事业单位财务指标", recentFIList);
			}
			// TODO 金融同业 刘鹏你来添加
			else if (currentCust.getCustType().equals("03")) {
				List<ReportData> recentFIList = customerService
						.findRecentReportData(lastOneYearReport,
								FinanFinancialIndexSheet.REPORTNO);

				List<ReportData> list1 = new LinkedList<ReportData>();
				List<ReportData> list2 = new LinkedList<ReportData>();
				List<ReportData> list3 = new LinkedList<ReportData>();
				List<ReportData> list4 = new LinkedList<ReportData>();
				List<ReportData> list5 = new LinkedList<ReportData>();
				List<ReportData> list6 = new LinkedList<ReportData>();

				for (ReportData rd : recentFIList) {
					if (rd.getDisplayFlag().substring(0, 1).equals("1")) {
						list1.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("2")) {
						list2.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("3")) {
						list3.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("4")) {
						list4.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("5")) {
						list5.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("6")) {
						list6.add(rd);
					}
				}

				recentFIMap.put("资产质量", list1);
				recentFIMap.put("盈利能力", list2);
				recentFIMap.put("资本充足率", list3);
				recentFIMap.put("流动性", list4);
				recentFIMap.put("规模", list5);
				recentFIMap.put("成长性", list6);

			} else {

			}
			model.addAttribute("lastOneFinancialIndex", recentFIMap);
		}

		// 前两年
		if (lastTwoYearReport != null) {
			// 企业法人
			Map<String, List<ReportData>> recentFIMap = new LinkedHashMap<String, List<ReportData>>();

			if (currentCust.getCustType().equals("01")) {
				List<ReportData> recentFIList;
				if (lastTwoYearReport.getReportType().equals("02")) {
					recentFIList = customerService.findRecentReportData(
							lastTwoYearReport, "0505");
				} else {
					recentFIList = customerService.findRecentReportData(
							lastTwoYearReport, "0105");
				}
				List<ReportData> list1 = new LinkedList<ReportData>();
				List<ReportData> list2 = new LinkedList<ReportData>();
				List<ReportData> list3 = new LinkedList<ReportData>();
				List<ReportData> list4 = new LinkedList<ReportData>();
				List<ReportData> list5 = new LinkedList<ReportData>();
				List<ReportData> list6 = new LinkedList<ReportData>();
				List<ReportData> list7 = new LinkedList<ReportData>();
				List<ReportData> list8 = new LinkedList<ReportData>();

				for (ReportData rd : recentFIList) {
					if (rd.getDisplayFlag().equals("1")) {
						list1.add(rd);
					} else if (rd.getDisplayFlag().equals("2")) {
						list2.add(rd);
					} else if (rd.getDisplayFlag().equals("3")) {
						list3.add(rd);
					} else if (rd.getDisplayFlag().equals("4")) {
						list4.add(rd);
					} else if (rd.getDisplayFlag().equals("5")) {
						list5.add(rd);
					} else if (rd.getDisplayFlag().equals("6")) {
						list6.add(rd);
					} else if (rd.getDisplayFlag().equals("7")) {
						list7.add(rd);
					} else if (rd.getDisplayFlag().equals("8")) {
						list8.add(rd);
					}

				}

				recentFIMap.put("短期偿债能力", list1);
				recentFIMap.put("长期偿债能力", list2);
				recentFIMap.put("营运能力", list3);
				recentFIMap.put("盈利能力", list4);
				recentFIMap.put("流动性", list5);
				recentFIMap.put("规模指标", list6);
				recentFIMap.put("财务结构", list7);
				recentFIMap.put("成长性", list8);
			}
			// 事业单位
			else if (currentCust.getCustType().equals("02")) {
				List<ReportData> recentFIList = customerService
						.findRecentReportData(lastTwoYearReport,
								PublicFinancialIndex.REPORTNO);
				recentFIMap.put("事业单位财务指标", recentFIList);
			}
			// TODO 金融同业 刘鹏你来添加
			else if (currentCust.getCustType().equals("03")) {
				List<ReportData> recentFIList = customerService
						.findRecentReportData(lastTwoYearReport,
								FinanFinancialIndexSheet.REPORTNO);

				List<ReportData> list1 = new LinkedList<ReportData>();
				List<ReportData> list2 = new LinkedList<ReportData>();
				List<ReportData> list3 = new LinkedList<ReportData>();
				List<ReportData> list4 = new LinkedList<ReportData>();
				List<ReportData> list5 = new LinkedList<ReportData>();
				List<ReportData> list6 = new LinkedList<ReportData>();

				for (ReportData rd : recentFIList) {
					if (rd.getDisplayFlag().substring(0, 1).equals("1")) {
						list1.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("2")) {
						list2.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("3")) {
						list3.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("4")) {
						list4.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("5")) {
						list5.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("6")) {
						list6.add(rd);
					}
				}

				recentFIMap.put("资产质量", list1);
				recentFIMap.put("盈利能力", list2);
				recentFIMap.put("资本充足率", list3);
				recentFIMap.put("流动性", list4);
				recentFIMap.put("规模", list5);
				recentFIMap.put("成长性", list6);

			} else {

			}
			model.addAttribute("lastTwoFinancialIndex", recentFIMap);

		}

		// 前三年
		if (lastThreeYearReport != null) {
			// 企业法人
			Map<String, List<ReportData>> recentFIMap = new LinkedHashMap<String, List<ReportData>>();

			if (currentCust.getCustType().equals("01")) {
				List<ReportData> recentFIList;
				if (lastThreeYearReport.getReportType().equals("02")) {
					recentFIList = customerService.findRecentReportData(
							lastThreeYearReport, "0505");
				} else {
					recentFIList = customerService.findRecentReportData(
							lastThreeYearReport, "0105");
				}
				List<ReportData> list1 = new LinkedList<ReportData>();
				List<ReportData> list2 = new LinkedList<ReportData>();
				List<ReportData> list3 = new LinkedList<ReportData>();
				List<ReportData> list4 = new LinkedList<ReportData>();
				List<ReportData> list5 = new LinkedList<ReportData>();
				List<ReportData> list6 = new LinkedList<ReportData>();
				List<ReportData> list7 = new LinkedList<ReportData>();
				List<ReportData> list8 = new LinkedList<ReportData>();

				for (ReportData rd : recentFIList) {
					if (rd.getDisplayFlag().equals("1")) {
						list1.add(rd);
					} else if (rd.getDisplayFlag().equals("2")) {
						list2.add(rd);
					} else if (rd.getDisplayFlag().equals("3")) {
						list3.add(rd);
					} else if (rd.getDisplayFlag().equals("4")) {
						list4.add(rd);
					} else if (rd.getDisplayFlag().equals("5")) {
						list5.add(rd);
					} else if (rd.getDisplayFlag().equals("6")) {
						list6.add(rd);
					} else if (rd.getDisplayFlag().equals("7")) {
						list7.add(rd);
					} else if (rd.getDisplayFlag().equals("8")) {
						list8.add(rd);
					}

				}

				recentFIMap.put("短期偿债能力", list1);
				recentFIMap.put("长期偿债能力", list2);
				recentFIMap.put("营运能力", list3);
				recentFIMap.put("盈利能力", list4);
				recentFIMap.put("流动性", list5);
				recentFIMap.put("规模指标", list6);
				recentFIMap.put("财务结构", list7);
				recentFIMap.put("成长性", list8);
			}
			// 事业单位
			else if (currentCust.getCustType().equals("02")) {
				List<ReportData> recentFIList = customerService
						.findRecentReportData(lastThreeYearReport,
								PublicFinancialIndex.REPORTNO);
				recentFIMap.put("事业单位财务指标", recentFIList);
			}
			// TODO 金融同业 刘鹏你来添加
			else if (currentCust.getCustType().equals("03")) {
				List<ReportData> recentFIList = customerService
						.findRecentReportData(lastThreeYearReport,
								FinanFinancialIndexSheet.REPORTNO);

				List<ReportData> list1 = new LinkedList<ReportData>();
				List<ReportData> list2 = new LinkedList<ReportData>();
				List<ReportData> list3 = new LinkedList<ReportData>();
				List<ReportData> list4 = new LinkedList<ReportData>();
				List<ReportData> list5 = new LinkedList<ReportData>();
				List<ReportData> list6 = new LinkedList<ReportData>();

				for (ReportData rd : recentFIList) {
					if (rd.getDisplayFlag().substring(0, 1).equals("1")) {
						list1.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("2")) {
						list2.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("3")) {
						list3.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("4")) {
						list4.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("5")) {
						list5.add(rd);
					} else if (rd.getDisplayFlag().substring(0, 1).equals("6")) {
						list6.add(rd);
					}
				}

				recentFIMap.put("资产质量", list1);
				recentFIMap.put("盈利能力", list2);
				recentFIMap.put("资本充足率", list3);
				recentFIMap.put("流动性", list4);
				recentFIMap.put("规模", list5);
				recentFIMap.put("成长性", list6);

			} else {

			}
			model.addAttribute("lastThreeFinancialIndex", recentFIMap);

		}

		model.addAttribute("lastDate", lastDate);
		model.addAttribute("lastOneDate", lastOneDate);
		model.addAttribute("lastTwoDate", lastTwoDate);
		model.addAttribute("lastThreeDate", lastThreeDate);

		return "finance/FinancialIndex_Trend";
	}

	// 对需要展示的结果进行重排，小计项提前，小项前加两个空格
	private List<ReportData> resortReportDataOrder(
			List<ReportData> reportDataList) {
		Integer size = 1;
		if (reportDataList.size() > 0)
			size = reportDataList.size();
		ReportData[] reportDataArray = (ReportData[]) reportDataList
				.toArray(new ReportData[size - 1]);
		// 记录小计项的起始位置
		int begin = 0;
		// 记录当前位置
		int current = 0;

		for (ReportData reportData : reportDataArray) {
			// 如果是小总计项
			if (reportData.getDisplayFlag().equals("11")) {
				ReportData temp = reportDataArray[begin];
				reportDataArray[begin] = reportDataArray[current];
				reportDataArray[current] = temp;
				begin = current + 1;
			}
			// 普通项 前面加两空格或者是-->号
			else if (reportData.getDisplayFlag().equals("10")) {
				reportData.setDisplayName("&emsp;-&nbsp;"
						+ reportData.getDisplayName());
			}
			// 如果是大总计项 则跳过 小项目的起始项+1
			else if (reportData.getDisplayFlag().equals("12")) {
				begin = current + 1;
			}
			current++;
		}

		return Arrays.asList(reportDataArray);
	}
}